{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考资料"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "中国科学院软件研究所 刘焕勇老师 https://github.com/liuhuanyong/QASystemOnMedicalKG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "from py2neo import Graph, Node\n",
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入数据集csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('medical_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(21, 23)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实体（节点）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有症状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "symptoms = []\n",
    "for each in df['症状']:\n",
    "    symptoms.extend(each.split(','))\n",
    "symptoms = set(symptoms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 所有科室"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "departments = []\n",
    "for each in df['科室']:\n",
    "    departments.extend(each.split(','))\n",
    "departments = set(departments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "checks = []\n",
    "for each in df['检查']:\n",
    "    checks.extend(each.split(','))\n",
    "checks = set(checks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "drugs = []\n",
    "for each in df['推荐药物']:\n",
    "    try:\n",
    "        drugs.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['常用药物']:\n",
    "    try:\n",
    "        drugs.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "drugs = set(drugs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有食物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "foods = []\n",
    "for each in df['可以吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['不可以吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['推荐吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "foods = set(foods)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有药物厂商"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "producers = []\n",
    "\n",
    "for each in df['具体药物']:\n",
    "    try:\n",
    "        for each_drug in each.split(','):\n",
    "            producer = each_drug.split('(')[0]\n",
    "            producers.append(producer)\n",
    "    except:\n",
    "        pass\n",
    "producers = set(producers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 疾病字典信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "disease_infos = [] # 疾病信息\n",
    "for idx, row in df.iterrows():\n",
    "    disease_infos.append(dict(row))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['疾病名称', '疾病描述', '疾病种类', '科室', '病因', '症状', '检查', '并发症', '花费', '疗程', '疗法', '治愈率', '易感人群', '感染概率', '感染途径', '预防措施', '推荐药物', '常用药物', '具体药物', '可以吃', '不可以吃', '推荐吃', '是否纳入医保'])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(row).keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关系（边）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def deduplicate(rels_old):\n",
    "    '''关系去重函数'''\n",
    "    rels_new = []\n",
    "    for each in rels_old:\n",
    "        if each not in rels_new:\n",
    "            rels_new.append(each)\n",
    "    return rels_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_check = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['检查'].split(','):\n",
    "        rels_check.append([row['疾病名称'], each])\n",
    "rels_check = deduplicate(rels_check)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '胸部CT检查'],\n",
       " ['肺泡蛋白质沉积症', '肺活检'],\n",
       " ['肺泡蛋白质沉积症', '支气管镜检查'],\n",
       " ['百日咳', '耳、鼻、咽拭子细菌培养'],\n",
       " ['百日咳', '周围血白细胞计数及分类检验'],\n",
       " ['百日咳', '血常规'],\n",
       " ['百日咳', '酶联免疫吸附试验'],\n",
       " ['百日咳', '白细胞分类计数'],\n",
       " ['苯中毒', '血常规'],\n",
       " ['苯中毒', '骨髓象分析'],\n",
       " ['苯中毒', '先令氏指数'],\n",
       " ['喘息样支气管炎', '肺部检查'],\n",
       " ['喘息样支气管炎', '肺和胸膜听诊'],\n",
       " ['喘息样支气管炎', '抗链球菌型M蛋白抗体'],\n",
       " ['喘息样支气管炎', '抗链球菌壁多糖抗体'],\n",
       " ['喘息样支气管炎', '酶联免疫吸附试验'],\n",
       " ['成人呼吸窘迫综合征', '胸部CT检查'],\n",
       " ['成人呼吸窘迫综合征', '呼吸肌功能测定'],\n",
       " ['成人呼吸窘迫综合征', '血浆蛋白C抗原'],\n",
       " ['成人呼吸窘迫综合征', '肺泡气-动脉血氧分压差'],\n",
       " ['成人呼吸窘迫综合征', '肺毛细血管楔压'],\n",
       " ['大量羊水吸入', '肺部检查'],\n",
       " ['大量羊水吸入', '胸部透视'],\n",
       " ['大量羊水吸入', '胸部平片'],\n",
       " ['大量羊水吸入', '胸部CT检查'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '痰液中细胞分类'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸部平片'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '痰液中寄生虫和虫卵'],\n",
       " ['大叶性肺炎', 'Optochin敏感试验'],\n",
       " ['大叶性肺炎', '小白鼠毒力试验'],\n",
       " ['大叶性肺炎', '痰培养'],\n",
       " ['大叶性肺炎', '肺活量体重指数'],\n",
       " ['大叶性肺炎', '胸部平片'],\n",
       " ['大叶性肺炎', '免疫电泳'],\n",
       " ['大叶性肺炎', '血常规'],\n",
       " ['大叶性肺炎', '痰液细菌涂片检查'],\n",
       " ['大楼病综合征', '钼靶X线检查'],\n",
       " ['大楼病综合征', 'CT检查'],\n",
       " ['大楼病综合征', '血常规'],\n",
       " ['二硫化碳中毒', '血常规'],\n",
       " ['二硫化碳中毒', '尿常规'],\n",
       " ['二硫化碳中毒', '肾功能检查'],\n",
       " ['二硫化碳中毒', '神经系统检查'],\n",
       " ['二硫化碳中毒', '眼底荧光血管造影'],\n",
       " ['二硫化碳中毒', '肌电图'],\n",
       " ['二硫化碳中毒', '脑电图检查'],\n",
       " ['二硫化碳中毒', '心肺功能运动试验（CPET）'],\n",
       " ['肺-胸膜阿米巴病', '胸部平片'],\n",
       " ['肺-胸膜阿米巴病', '痰液病原体检查'],\n",
       " ['肺出血－肾炎综合征', '痰液常规检查'],\n",
       " ['肺出血－肾炎综合征', '尿液镜检法'],\n",
       " ['肺出血－肾炎综合征', '胸部平片'],\n",
       " ['肺出血－肾炎综合征', '肾功能检查'],\n",
       " ['肺出血－肾炎综合征', '抗肾小球基底膜抗体测定（AGBM）'],\n",
       " ['肺出血－肾炎综合征', '隐血试验与含铁血黄素检查'],\n",
       " ['肺放线菌病', '痰液病原体检查'],\n",
       " ['肺放线菌病', '胸部平片'],\n",
       " ['肺泡蛋白沉着症', '胸部CT检查'],\n",
       " ['肺泡蛋白沉着症', '支气管镜检查'],\n",
       " ['肺泡蛋白沉着症', '肺活检'],\n",
       " ['肺曲菌病', '胸部平片'],\n",
       " ['肺曲菌病', '痰培养'],\n",
       " ['肺曲菌病', '纤维支气管镜检查'],\n",
       " ['肺曲菌病', '胸部CT检查'],\n",
       " ['肺曲菌病', '痰液细菌涂片检查'],\n",
       " ['放射性肺炎', '胸部平片'],\n",
       " ['放射性肺炎', '肺功能检查'],\n",
       " ['放射性肺炎', '通气与血流灌注比值（V/Q）'],\n",
       " ['放射性肺炎', '每分钟最大通气量（MVV）'],\n",
       " ['放射性肺炎', '血常规'],\n",
       " ['肺念珠菌病', '肺量计检测'],\n",
       " ['肺念珠菌病', '胸部平片'],\n",
       " ['肺念珠菌病', '痰液细菌涂片检查'],\n",
       " ['肺大疱', '胸部CT检查'],\n",
       " ['肺大疱', '支气管造影'],\n",
       " ['肺大疱', '胸部平片'],\n",
       " ['肺炎球菌肺炎', '胸部平片'],\n",
       " ['肺炎球菌肺炎', '血常规'],\n",
       " ['肺气肿', '肺量计检测'],\n",
       " ['肺气肿', '残气量／肺总量比值（RV/TLC）'],\n",
       " ['肺气肿', '肺容量测定'],\n",
       " ['肺气肿', '肺活量（VC）'],\n",
       " ['肺气肿', '最大呼气流量-容积曲线（MEFV）'],\n",
       " ['肺气肿', '深吸气量（IC）'],\n",
       " ['肺气肿', '一秒用力呼出量／用力肺活量比值'],\n",
       " ['肺气肿', '心电图'],\n",
       " ['肺气肿', '无效腔气量／潮气量比值'],\n",
       " ['肺气肿', '肺泡气-动脉血氧分压差'],\n",
       " ['肺炎杆菌肺炎', '痰液细菌涂片检查'],\n",
       " ['肺炎杆菌肺炎', '痰液细菌培养'],\n",
       " ['肺炎杆菌肺炎', '血常规']]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_check"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-症状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_symptom = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['症状'].split(','):\n",
    "        rels_symptom.append([row['疾病名称'], each])\n",
    "rels_symptom = deduplicate(rels_symptom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '紫绀'],\n",
       " ['肺泡蛋白质沉积症', '胸痛'],\n",
       " ['肺泡蛋白质沉积症', '呼吸困难'],\n",
       " ['肺泡蛋白质沉积症', '乏力'],\n",
       " ['肺泡蛋白质沉积症', '毓卓'],\n",
       " ['百日咳', '吸气时有蝉鸣音'],\n",
       " ['百日咳', '痉挛性咳嗽'],\n",
       " ['百日咳', '胸闷'],\n",
       " ['百日咳', '肺阴虚'],\n",
       " ['百日咳', '抽搐'],\n",
       " ['百日咳', '低热'],\n",
       " ['百日咳', '闫鹏辉'],\n",
       " ['百日咳', '惊厥'],\n",
       " ['苯中毒', '恶心'],\n",
       " ['苯中毒', '抽搐'],\n",
       " ['苯中毒', '感觉障碍'],\n",
       " ['喘息样支气管炎', '耸肩喘息'],\n",
       " ['喘息样支气管炎', '哮鸣音'],\n",
       " ['喘息样支气管炎', '纤毛上皮细胞损伤脱落'],\n",
       " ['喘息样支气管炎', '变应性咳嗽'],\n",
       " ['喘息样支气管炎', '化学性支气管炎'],\n",
       " ['喘息样支气管炎', '喘息'],\n",
       " ['喘息样支气管炎', '冬春季的慢性咳...'],\n",
       " ['喘息样支气管炎', '咳嗽伴哮鸣音'],\n",
       " ['成人呼吸窘迫综合征', '呼吸困难'],\n",
       " ['成人呼吸窘迫综合征', '紫绀'],\n",
       " ['成人呼吸窘迫综合征', '心源性呼吸窘迫'],\n",
       " ['大量羊水吸入', '面色青紫'],\n",
       " ['大量羊水吸入', '呼吸困难'],\n",
       " ['大量羊水吸入', '口唇青紫'],\n",
       " ['大量羊水吸入', '肺纹理增粗'],\n",
       " ['大量羊水吸入', '出生后即有持续青紫'],\n",
       " ['大量羊水吸入', '口唇和甲床略带青紫'],\n",
       " ['大量羊水吸入', '气急'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '嗜酸性粒细胞增多'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '咽部异物感'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸闷憋气'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '咯血伴发热'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸闷'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '乏力'],\n",
       " ['大叶性肺炎', '湿啰音'],\n",
       " ['大叶性肺炎', '胸痛'],\n",
       " ['大叶性肺炎', '发烧'],\n",
       " ['大叶性肺炎', '咳铁锈色痰'],\n",
       " ['大叶性肺炎', '急性面容'],\n",
       " ['大叶性肺炎', '呼吸音减弱'],\n",
       " ['大楼病综合征', '紧张性头晕'],\n",
       " ['大楼病综合征', '紧张性头痛'],\n",
       " ['大楼病综合征', '间歇性头晕'],\n",
       " ['大楼病综合征', '鼻塞'],\n",
       " ['大楼病综合征', '头晕'],\n",
       " ['大楼病综合征', '恶心'],\n",
       " ['大楼病综合征', '眼睛痒'],\n",
       " ['大楼病综合征', '目赤'],\n",
       " ['大楼病综合征', '间歇性头痛'],\n",
       " ['二硫化碳中毒', '昏迷'],\n",
       " ['二硫化碳中毒', '腱反射消失'],\n",
       " ['二硫化碳中毒', '呕吐'],\n",
       " ['二硫化碳中毒', '谵妄'],\n",
       " ['二硫化碳中毒', '多发性神经炎'],\n",
       " ['二硫化碳中毒', '浅感觉减退或缺失'],\n",
       " ['二硫化碳中毒', '恶心'],\n",
       " ['二硫化碳中毒', '感觉障碍'],\n",
       " ['肺-胸膜阿米巴病', '盗汗'],\n",
       " ['肺-胸膜阿米巴病', '腹泻'],\n",
       " ['肺-胸膜阿米巴病', '乏力'],\n",
       " ['肺-胸膜阿米巴病', '发热伴咳嗽、咯...'],\n",
       " ['肺出血－肾炎综合征', '肺部出血'],\n",
       " ['肺出血－肾炎综合征', '呼吸困难'],\n",
       " ['肺出血－肾炎综合征', '咯血'],\n",
       " ['肺出血－肾炎综合征', '气短'],\n",
       " ['肺出血－肾炎综合征', '发热伴咳嗽、咯...'],\n",
       " ['肺出血－肾炎综合征', '蛋白尿'],\n",
       " ['肺放线菌病', '骨膜炎'],\n",
       " ['肺放线菌病', '体重减轻'],\n",
       " ['肺放线菌病', '咯血'],\n",
       " ['肺放线菌病', '盗汗'],\n",
       " ['肺放线菌病', '乏力'],\n",
       " ['肺放线菌病', '低热'],\n",
       " ['肺泡蛋白沉着症', '胸痛'],\n",
       " ['肺泡蛋白沉着症', '紫绀'],\n",
       " ['肺泡蛋白沉着症', '呼吸困难'],\n",
       " ['肺泡蛋白沉着症', '肺泡炎症'],\n",
       " ['肺泡蛋白沉着症', '肺泡灌洗液可见...'],\n",
       " ['肺泡蛋白沉着症', '乏力'],\n",
       " ['肺泡蛋白沉着症', '发热伴咳嗽、咯...'],\n",
       " ['肺曲菌病', '咳出棕色痰栓'],\n",
       " ['肺曲菌病', '嗜酸性粒细胞增多'],\n",
       " ['肺曲菌病', '胸痛'],\n",
       " ['放射性肺炎', '闫铁'],\n",
       " ['放射性肺炎', '肺纤维化'],\n",
       " ['放射性肺炎', '胸痛'],\n",
       " ['放射性肺炎', '低热'],\n",
       " ['肺念珠菌病', '痰呈粘液脓性'],\n",
       " ['肺念珠菌病', '肺部啰音'],\n",
       " ['肺大疱', '胸闷'],\n",
       " ['肺大疱', '气短'],\n",
       " ['肺大疱', '胸闷憋气'],\n",
       " ['肺大疱', '发热伴咳嗽、咯...'],\n",
       " ['肺炎球菌肺炎', '痰中带血丝'],\n",
       " ['肺炎球菌肺炎', '咳嗽伴胸痛'],\n",
       " ['肺炎球菌肺炎', '胸痛'],\n",
       " ['肺炎球菌肺炎', '发热伴咳嗽、咯...'],\n",
       " ['肺气肿', '纵隔浊音界扩大'],\n",
       " ['肺气肿', '桶状胸'],\n",
       " ['肺气肿', '小支气管粘膜水肿'],\n",
       " ['肺气肿', '黏稠或脓性痰伴...'],\n",
       " ['肺气肿', '横膈低平'],\n",
       " ['肺气肿', '胸闷'],\n",
       " ['肺气肿', '呼气音延长'],\n",
       " ['肺气肿', '肺部啰音'],\n",
       " ['肺气肿', '呼吸音减弱'],\n",
       " ['肺炎杆菌肺炎', '畏寒'],\n",
       " ['肺炎杆菌肺炎', '胸痛'],\n",
       " ['肺炎杆菌肺炎', '气急'],\n",
       " ['肺炎杆菌肺炎', '发热伴咳嗽、咯...']]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_symptom"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-疾病（并发症）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "rels_acompany = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['并发症'].split(','):\n",
    "        rels_acompany.append([row['疾病名称'], each])\n",
    "rels_acompany = deduplicate(rels_acompany)\n",
    "print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '多重肺部感染'],\n",
       " ['百日咳', '肺不张'],\n",
       " ['苯中毒', '贫血'],\n",
       " ['喘息样支气管炎', '支气管哮喘'],\n",
       " ['成人呼吸窘迫综合征', '细菌性肺炎'],\n",
       " ['大量羊水吸入', '呼吸衰竭'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胆道蛔虫病'],\n",
       " ['大叶性肺炎', '脓胸'],\n",
       " ['大楼病综合征', '抑郁症'],\n",
       " ['二硫化碳中毒', '昏迷'],\n",
       " ['肺-胸膜阿米巴病', '阿米巴肝脓肿'],\n",
       " ['肺出血－肾炎综合征', '便血'],\n",
       " ['肺放线菌病', '膈下脓肿'],\n",
       " ['肺泡蛋白沉着症', '呼吸衰竭'],\n",
       " ['肺曲菌病', '过敏性鼻炎'],\n",
       " ['放射性肺炎', '肺气肿'],\n",
       " ['肺念珠菌病', '菌血症'],\n",
       " ['肺大疱', '张力性气胸'],\n",
       " ['肺炎球菌肺炎', '败血症'],\n",
       " ['肺气肿', '呼吸衰竭'],\n",
       " ['肺气肿', '自发性气胸'],\n",
       " ['肺气肿', '慢性肺源性心脏病'],\n",
       " ['肺气肿', '胃溃疡'],\n",
       " ['肺炎杆菌肺炎', '脑疝']]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_acompany"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-推荐药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_recommanddrug = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['推荐药物'].split(','):\n",
    "            rels_recommanddrug.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_recommanddrug = deduplicate(rels_recommanddrug)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-常用药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_commonddrug = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['常用药物'].split(','):\n",
    "            rels_commonddrug.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_commonddrug = deduplicate(rels_commonddrug)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-不可以吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_noteat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['不可以吃'].split(','):\n",
    "            rels_noteat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_noteat = deduplicate(rels_noteat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-可以吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_doeat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['可以吃'].split(','):\n",
    "            rels_doeat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_doeat = deduplicate(rels_doeat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-推荐吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_recommandeat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['推荐吃'].split(','):\n",
    "            rels_recommandeat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_recommandeat = deduplicate(rels_recommandeat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：药物厂商-具体药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_drug_producer = []\n",
    "for each in df['具体药物']:\n",
    "    try:\n",
    "        for each_drug in each.split(','):\n",
    "            producer = each_drug.split('(')[0]\n",
    "            drug = each_drug.split('(')[1][:-1]\n",
    "            rels_drug_producer.append([producer, drug])\n",
    "    except:\n",
    "        pass\n",
    "rels_drug_producer = deduplicate(rels_drug_producer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-科室、小科室-大科室"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_category = [] # 关系：疾病-科室\n",
    "rels_department = [] # 关系：小科室-大科室\n",
    "for idx, row in df.iterrows():\n",
    "    if len(row['科室'].split(',')) == 1:\n",
    "        rels_category.append([row['疾病名称'], row['科室']])\n",
    "    else:\n",
    "        big = row['科室'].split(',')[0] # 大科室\n",
    "        small = row['科室'].split(',')[1] # 小科室\n",
    "        rels_category.append([row['疾病名称'], small])\n",
    "        rels_department.append([small, big])\n",
    "rels_category = deduplicate(rels_category)\n",
    "rels_department = deduplicate(rels_department)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '呼吸内科'],\n",
       " ['百日咳', '小儿内科'],\n",
       " ['苯中毒', '急诊科'],\n",
       " ['喘息样支气管炎', '呼吸内科'],\n",
       " ['成人呼吸窘迫综合征', '呼吸内科'],\n",
       " ['大量羊水吸入', '小儿内科'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '呼吸内科'],\n",
       " ['大叶性肺炎', '呼吸内科'],\n",
       " ['大楼病综合征', '其他综合'],\n",
       " ['二硫化碳中毒', '急诊科'],\n",
       " ['肺-胸膜阿米巴病', '呼吸内科'],\n",
       " ['肺出血－肾炎综合征', '呼吸内科'],\n",
       " ['肺放线菌病', '呼吸内科'],\n",
       " ['肺泡蛋白沉着症', '呼吸内科'],\n",
       " ['肺曲菌病', '呼吸内科'],\n",
       " ['放射性肺炎', '呼吸内科'],\n",
       " ['肺念珠菌病', '呼吸内科'],\n",
       " ['肺大疱', '呼吸内科'],\n",
       " ['肺炎球菌肺炎', '呼吸内科'],\n",
       " ['肺气肿', '呼吸内科'],\n",
       " ['肺炎杆菌肺炎', '呼吸内科']]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['呼吸内科', '内科'], ['小儿内科', '儿科'], ['其他综合', '其他科室']]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_department"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 连接图数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 注意，这里的用户名为neo4j全局用户名，而非DBMS或者database的名称\n",
    "g = Graph('http://localhost:7474', auth=('neo4j', '123456'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建知识图谱实体（节点）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Neo4j样例代码\n",
    "\n",
    "# 创建实体\n",
    "# node = Node('Disease', name='百日咳', easy_get='多见于小儿')\n",
    "# g.create(node)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "(No data)"
      ],
      "text/plain": [
       "(No data)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除所有实体和关系\n",
    "cypher = 'MATCH (n) DETACH DELETE n'\n",
    "g.run(cypher)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建疾病实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建疾病实体： 百日咳\n",
      "创建疾病实体： 苯中毒\n",
      "创建疾病实体： 喘息样支气管炎\n",
      "创建疾病实体： 成人呼吸窘迫综合征\n",
      "创建疾病实体： 大量羊水吸入\n",
      "创建疾病实体： 单纯性肺嗜酸粒细胞浸润症\n",
      "创建疾病实体： 大叶性肺炎\n",
      "创建疾病实体： 大楼病综合征\n",
      "创建疾病实体： 二硫化碳中毒\n",
      "创建疾病实体： 肺-胸膜阿米巴病\n",
      "创建疾病实体： 肺出血－肾炎综合征\n",
      "创建疾病实体： 肺放线菌病\n",
      "创建疾病实体： 肺泡蛋白沉着症\n",
      "创建疾病实体： 肺曲菌病\n",
      "创建疾病实体： 放射性肺炎\n",
      "创建疾病实体： 肺念珠菌病\n",
      "创建疾病实体： 肺大疱\n",
      "创建疾病实体： 肺炎球菌肺炎\n",
      "创建疾病实体： 肺气肿\n",
      "创建疾病实体： 肺炎杆菌肺炎\n",
      "共创建 20 个疾病实体\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "for disease_dict in disease_infos:\n",
    "    try:\n",
    "        node = Node(\"Disease\",\n",
    "                    name=disease_dict['疾病名称'],\n",
    "                    desc=disease_dict['疾病描述'],\n",
    "                    prevent=disease_dict['预防措施'],\n",
    "                    cause=disease_dict['病因'],\n",
    "                    easy_get=disease_dict['易感人群'],\n",
    "                    cure_lasttime=disease_dict['疗程'],\n",
    "                    cure_department=disease_dict['科室'],\n",
    "                    cure_way=disease_dict['疗法'], \n",
    "                    cured_prob=disease_dict['治愈率'])\n",
    "        g.create(node)\n",
    "        count += 1\n",
    "        print('创建疾病实体：', disease_dict['疾病名称'])\n",
    "    except:\n",
    "        pass\n",
    "print('共创建 {} 个疾病实体'.format(count))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建药物实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 头孢克洛颗粒\n",
      "创建实体 环酯红霉素片\n",
      "创建实体 盐酸克林霉素棕榈酸酯颗粒\n",
      "创建实体 头孢拉定胶囊\n",
      "创建实体 注射用硫代硫酸钠\n",
      "创建实体 白及颗粒\n",
      "创建实体 盐酸左氧氟沙星胶囊\n",
      "创建实体 头孢克肟分散片\n",
      "创建实体 枸橼酸喷托维林片\n",
      "创建实体 小青龙合剂\n",
      "创建实体 甲硝唑维B6片\n",
      "创建实体 琥乙红霉素片\n",
      "创建实体 琥乙红霉素颗粒\n",
      "创建实体 头孢氨苄胶囊\n",
      "创建实体 注射用盐酸氨溴索\n",
      "创建实体 酮康唑乳膏\n",
      "创建实体 硫酸特布他林片\n",
      "创建实体 阿奇霉素胶囊\n",
      "创建实体 硝酸益康唑乳膏\n",
      "创建实体 注射用美洛西林钠舒巴坦钠\n",
      "创建实体 托西酸舒他西林胶囊\n",
      "创建实体 盐酸克林霉素棕榈酸酯分散片\n",
      "创建实体 阿莫西林颗粒\n",
      "创建实体 盐酸利多卡因注射液\n",
      "创建实体 胸腺肽肠溶片\n",
      "创建实体 氨苄西林胶囊\n",
      "创建实体 盐酸特比萘芬片\n",
      "创建实体 头孢氨苄甲氧苄啶片\n",
      "创建实体 硝酸咪康唑乳膏\n",
      "创建实体 氨溴特罗口服溶液\n",
      "创建实体 注射用布美他尼\n",
      "创建实体 盐酸班布特罗胶囊\n",
      "创建实体 盐酸氨溴索葡萄糖注射液\n",
      "创建实体 仁青芒觉\n",
      "创建实体 注射用还原型谷胱甘肽钠\n",
      "创建实体 红霉素肠溶片\n",
      "创建实体 依托红霉素片\n",
      "创建实体 注射用磺苄西林钠\n",
      "创建实体 乙酰螺旋霉素片\n",
      "创建实体 硝酸咪康唑栓\n",
      "创建实体 茶碱缓释胶囊\n",
      "创建实体 喷托维林氯化铵糖浆\n",
      "创建实体 硫酸沙丁胺醇片\n",
      "创建实体 蛤蚧定喘丸\n",
      "创建实体 布美他尼片\n",
      "创建实体 阿奇霉素片\n",
      "创建实体 硫酸特布他林雾化液\n",
      "创建实体 硫酸沙丁胺醇气雾剂\n",
      "创建实体 人血白蛋白\n",
      "创建实体 转移因子口服溶液\n",
      "创建实体 氟康唑胶囊\n",
      "创建实体 噻托溴铵粉吸入剂\n",
      "创建实体 头孢丙烯分散片\n",
      "创建实体 替硝唑片\n",
      "创建实体 盐酸班布特罗片\n",
      "创建实体 伊曲康唑胶囊\n",
      "创建实体 如意定喘丸\n",
      "创建实体 茶碱缓释片\n",
      "创建实体 化痰平喘片\n",
      "创建实体 喘嗽宁片\n",
      "创建实体 克霉唑阴道片\n",
      "创建实体 阿奇霉素分散片\n",
      "创建实体 二羟丙茶碱片\n",
      "创建实体 蒺藜药酒\n",
      "创建实体 注射用呋塞米\n",
      "创建实体 麦迪霉素片\n",
      "创建实体 注射用甲泼尼龙琥珀酸钠\n",
      "创建实体 小青龙颗粒\n",
      "创建实体 奥硝唑片\n",
      "创建实体 注射用鼠神经生长因子\n",
      "创建实体 头孢拉定分散片\n",
      "创建实体 注射用头孢唑林钠\n",
      "创建实体 雷公藤多苷片\n",
      "创建实体 甲泼尼龙片\n",
      "创建实体 头孢泊肟酯胶囊\n",
      "创建实体 诺氟沙星胶囊\n",
      "创建实体 通宣理肺丸\n",
      "创建实体 肺气肿片\n",
      "创建实体 穿心莲内酯片\n",
      "创建实体 噻托溴铵粉雾剂\n",
      "创建实体 盐酸克林霉素胶囊\n",
      "创建实体 祛痰止咳胶囊\n",
      "创建实体 乳酸左氧氟沙星片\n",
      "创建实体 复方蛤青胶囊\n",
      "创建实体 阿奇霉素颗粒\n",
      "创建实体 环吡酮胺乳膏\n",
      "创建实体 蛇胆川贝液\n",
      "创建实体 布地奈德气雾剂\n",
      "创建实体 氨茶碱片\n",
      "创建实体 注射用盐酸头孢替安\n",
      "创建实体 联苯苄唑乳膏\n",
      "创建实体 盐酸特比萘芬溶液\n",
      "创建实体 注射用盐酸溴己新\n",
      "创建实体 司帕沙星分散片\n",
      "创建实体 盐酸特比萘芬凝胶\n",
      "创建实体 云南花粉片\n",
      "创建实体 百咳静糖浆\n",
      "创建实体 硝酸益康唑溶液\n",
      "创建实体 十一味金色丸\n",
      "创建实体 头孢克肟颗粒\n",
      "创建实体 盐酸妥洛特罗片\n",
      "创建实体 甲硝唑片\n",
      "创建实体 氯霉素片\n",
      "创建实体 地塞米松磷酸钠注射液\n",
      "创建实体 大月晶丸\n"
     ]
    }
   ],
   "source": [
    "for each in drugs:\n",
    "    node = Node('Drug', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建食物实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 番茄牛肉\n",
      "创建实体 茄子\n",
      "创建实体 凉拌莲藕\n",
      "创建实体 咖啡\n",
      "创建实体 百合糖粥\n",
      "创建实体 海参（水浸）\n",
      "创建实体 虾皮香芹燕麦粥\n",
      "创建实体 百合花生汤\n",
      "创建实体 鸡\n",
      "创建实体 小黄瓜凉拌面\n",
      "创建实体 猪肉(瘦)\n",
      "创建实体 圆白菜\n",
      "创建实体 白菜蛋花粥\n",
      "创建实体 南瓜子仁\n",
      "创建实体 凉拌番茄\n",
      "创建实体 牛奶玉米汤\n",
      "创建实体 番茄羊肉汤\n",
      "创建实体 拌肚丝白菜\n",
      "创建实体 山楂糕\n",
      "创建实体 鲢鱼\n",
      "创建实体 海蚌\n",
      "创建实体 海螺\n",
      "创建实体 鲜芦根粥\n",
      "创建实体 罗汉果雪耳鸡汤\n",
      "创建实体 黄瓜拌豆腐丝\n",
      "创建实体 辣椒(青、尖)\n",
      "创建实体 百合汤\n",
      "创建实体 萝卜豆腐汤\n",
      "创建实体 空心菜\n",
      "创建实体 黄瓜拌兔丝\n",
      "创建实体 清蒸鸡蛋羹\n",
      "创建实体 油麦菜\n",
      "创建实体 奶油\n",
      "创建实体 银耳粥\n",
      "创建实体 山楂红糖粥\n",
      "创建实体 椰子银耳煲鸽汤\n",
      "创建实体 玉竹白菜\n",
      "创建实体 红烧鲢鱼\n",
      "创建实体 松子仁\n",
      "创建实体 狗肉\n",
      "创建实体 番茄猪肝瘦肉汤\n",
      "创建实体 山楂乌梅饮\n",
      "创建实体 北杏炖雪梨\n",
      "创建实体 奶汤锅子鱼\n",
      "创建实体 栗子（熟）\n",
      "创建实体 辣椒(红、尖、干)\n",
      "创建实体 鹿肉\n",
      "创建实体 鸡肝\n",
      "创建实体 黄芪粥\n",
      "创建实体 雪梨饮\n",
      "创建实体 黄瓜三丝汤\n",
      "创建实体 素炒小白菜\n",
      "创建实体 意式番茄牛肉凉面\n",
      "创建实体 螃蟹\n",
      "创建实体 鸡蛋清\n",
      "创建实体 白酒\n",
      "创建实体 番茄皮蛋汤\n",
      "创建实体 鲜百合烧肉\n",
      "创建实体 红枣银耳粥\n",
      "创建实体 番茄汁\n",
      "创建实体 核桃\n",
      "创建实体 川贝酿梨\n",
      "创建实体 香菜\n",
      "创建实体 黄瓜拌皮丝\n",
      "创建实体 鲫鱼\n",
      "创建实体 莲子\n",
      "创建实体 冬瓜草鱼汤\n",
      "创建实体 酱豆腐汁烧猪肉\n",
      "创建实体 番茄\n",
      "创建实体 三鲜冬瓜汤\n",
      "创建实体 水萝卜拌黄瓜\n",
      "创建实体 大蒜\n",
      "创建实体 鸭梨粥\n",
      "创建实体 薏仁党参粥\n",
      "创建实体 小黄瓜西瓜汁\n",
      "创建实体 冬菇烧白菜\n",
      "创建实体 锅塌番茄\n",
      "创建实体 菊花芦根茶\n",
      "创建实体 番茄鸡蛋煎饼\n",
      "创建实体 荞麦粥\n",
      "创建实体 番茄三素\n",
      "创建实体 黄瓜拌香菜\n",
      "创建实体 糖醋草鱼\n",
      "创建实体 海虾\n",
      "创建实体 首乌百合粥\n",
      "创建实体 竹笋\n",
      "创建实体 山楂百合汤\n",
      "创建实体 山药\n",
      "创建实体 小麦粥\n",
      "创建实体 豆薯小米粥\n",
      "创建实体 生姜乌梅饮\n",
      "创建实体 赤贝\n",
      "创建实体 花生杏仁粥\n",
      "创建实体 泡莲藕\n",
      "创建实体 百合炖猪蹄\n",
      "创建实体 紫苏粥\n",
      "创建实体 啤酒\n",
      "创建实体 番茄西米粥\n",
      "创建实体 百合粥\n",
      "创建实体 瓜粒杂锦汤\n",
      "创建实体 山楂桑椹粥\n",
      "创建实体 洋葱\n",
      "创建实体 绿豆南瓜汤\n",
      "创建实体 番茄芹菜汁\n",
      "创建实体 大豆\n",
      "创建实体 百合双耳鸡蛋羹\n",
      "创建实体 百合炒芦笋\n",
      "创建实体 羊腩萝卜丝粥\n",
      "创建实体 苦瓜\n",
      "创建实体 茭白\n",
      "创建实体 豆浆\n",
      "创建实体 猪肉(肥瘦)\n",
      "创建实体 豆腐干炒韭菜\n",
      "创建实体 鲈鱼\n",
      "创建实体 鲤鱼\n",
      "创建实体 百合汁\n",
      "创建实体 党参粥\n",
      "创建实体 乌梅粥\n",
      "创建实体 樱桃番茄\n",
      "创建实体 水萝卜丝拌白菜\n",
      "创建实体 杏仁\n",
      "创建实体 海蟹\n",
      "创建实体 南瓜\n",
      "创建实体 生菜\n",
      "创建实体 鸡蛋木耳粥\n",
      "创建实体 番茄羊肉蛋花汤\n",
      "创建实体 鱼腥草\n",
      "创建实体 番茄苹果芝麻蜜\n",
      "创建实体 蔬菜杂烩粥\n",
      "创建实体 肉末冬瓜\n",
      "创建实体 带鱼\n",
      "创建实体 生芦根粥\n",
      "创建实体 羊肉(瘦)\n",
      "创建实体 小白菜\n",
      "创建实体 冰糖银耳羹\n",
      "创建实体 番茄牛肉片\n",
      "创建实体 薏米莲子粥\n",
      "创建实体 排骨汤\n",
      "创建实体 鸡蛋\n",
      "创建实体 油菜\n",
      "创建实体 珊瑚白菜墩\n",
      "创建实体 杂粮粥\n",
      "创建实体 五丝白菜卷\n",
      "创建实体 韭菜\n",
      "创建实体 炖白鲢鱼\n",
      "创建实体 肉松\n"
     ]
    }
   ],
   "source": [
    "for each in foods:\n",
    "    node = Node('Food', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建检查实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 肺泡气-动脉血氧分压差\n",
      "创建实体 痰液常规检查\n",
      "创建实体 钼靶X线检查\n",
      "创建实体 纤维支气管镜检查\n",
      "创建实体 耳、鼻、咽拭子细菌培养\n",
      "创建实体 痰培养\n",
      "创建实体 尿常规\n",
      "创建实体 痰液病原体检查\n",
      "创建实体 先令氏指数\n",
      "创建实体 支气管镜检查\n",
      "创建实体 肺活量体重指数\n",
      "创建实体 眼底荧光血管造影\n",
      "创建实体 通气与血流灌注比值（V/Q）\n",
      "创建实体 每分钟最大通气量（MVV）\n",
      "创建实体 痰液细菌培养\n",
      "创建实体 肺和胸膜听诊\n",
      "创建实体 血浆蛋白C抗原\n",
      "创建实体 心肺功能运动试验（CPET）\n",
      "创建实体 肺毛细血管楔压\n",
      "创建实体 骨髓象分析\n",
      "创建实体 痰液细菌涂片检查\n",
      "创建实体 肺量计检测\n",
      "创建实体 CT检查\n",
      "创建实体 呼吸肌功能测定\n",
      "创建实体 Optochin敏感试验\n",
      "创建实体 隐血试验与含铁血黄素检查\n",
      "创建实体 深吸气量（IC）\n",
      "创建实体 残气量／肺总量比值（RV/TLC）\n",
      "创建实体 免疫电泳\n",
      "创建实体 支气管造影\n",
      "创建实体 无效腔气量／潮气量比值\n",
      "创建实体 心电图\n",
      "创建实体 痰液中寄生虫和虫卵\n",
      "创建实体 肺活检\n",
      "创建实体 血常规\n",
      "创建实体 脑电图检查\n",
      "创建实体 一秒用力呼出量／用力肺活量比值\n",
      "创建实体 尿液镜检法\n",
      "创建实体 神经系统检查\n",
      "创建实体 肌电图\n",
      "创建实体 肺功能检查\n",
      "创建实体 胸部CT检查\n",
      "创建实体 周围血白细胞计数及分类检验\n",
      "创建实体 白细胞分类计数\n",
      "创建实体 肾功能检查\n",
      "创建实体 肺部检查\n",
      "创建实体 抗链球菌型M蛋白抗体\n",
      "创建实体 肺活量（VC）\n",
      "创建实体 酶联免疫吸附试验\n",
      "创建实体 小白鼠毒力试验\n",
      "创建实体 胸部透视\n",
      "创建实体 抗链球菌壁多糖抗体\n",
      "创建实体 最大呼气流量-容积曲线（MEFV）\n",
      "创建实体 肺容量测定\n",
      "创建实体 抗肾小球基底膜抗体测定（AGBM）\n",
      "创建实体 痰液中细胞分类\n",
      "创建实体 胸部平片\n"
     ]
    }
   ],
   "source": [
    "for each in checks:\n",
    "    node = Node('Check', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建科室实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 小儿内科\n",
      "创建实体 急诊科\n",
      "创建实体 其他科室\n",
      "创建实体 内科\n",
      "创建实体 儿科\n",
      "创建实体 呼吸内科\n",
      "创建实体 其他综合\n"
     ]
    }
   ],
   "source": [
    "for each in departments:\n",
    "    node = Node('Department', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建 药物厂商 实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 哈药总厂托西酸舒他西林胶囊\n",
      "创建实体 宜昌长江阿奇霉素分散片\n",
      "创建实体 湘江药业阿奇霉素颗粒\n",
      "创建实体 苏州三药盐酸克林霉素胶囊\n",
      "创建实体 杭州民生药业盐酸克林霉素胶\n",
      "创建实体 白敬宇制药氨茶碱片\n",
      "创建实体 瑞阳制药注射用磺苄西林钠\n",
      "创建实体 皇象铁力蓝天甲硝唑维B6片\n",
      "创建实体 南光注射用甲泼尼龙琥珀酸钠\n",
      "创建实体 盛翔制药蛇胆川贝液\n",
      "创建实体 北京京丰制药集团诺氟沙星胶\n",
      "创建实体 国药国瑞注射用盐酸溴己新\n",
      "创建实体 佳乐弗乳酸左氧氟沙星片\n",
      "创建实体 桂林华信环吡酮胺乳膏\n",
      "创建实体 纽兰药业小青龙颗粒\n",
      "创建实体 北京益民药业盐酸利多卡因注\n",
      "创建实体 申优\n",
      "创建实体 联邦安必仙氨苄西林胶囊\n",
      "创建实体 搏沃欣\n",
      "创建实体 康美药业诺氟沙星胶囊\n",
      "创建实体 康美药业甲硝唑片\n",
      "创建实体 万方制药肺气肿片\n",
      "创建实体 爱普森药业阿奇霉素片\n",
      "创建实体 中国大冢盐酸利多卡因注射液\n",
      "创建实体 安诺药业阿奇霉素分散片\n",
      "创建实体 广东华南依托红霉素片\n",
      "创建实体 白云山医药诺氟沙星胶囊\n",
      "创建实体 仁和堂硫酸沙丁胺醇片\n",
      "创建实体 湖南九典制药盐酸班布特罗胶\n",
      "创建实体 康普药业盐酸克林霉素胶囊\n",
      "创建实体 华瑞联合制药头孢氨苄甲氧苄\n",
      "创建实体 恒益药业琥乙红霉素颗粒\n",
      "创建实体 复仙安\n",
      "创建实体 迪瑞制药琥乙红霉素颗粒\n",
      "创建实体 海南制药厂蛇胆川贝液\n",
      "创建实体 江苏长江阿奇霉素胶囊\n",
      "创建实体 白云山光华盐酸克林霉素胶囊\n",
      "创建实体 石药欧意诺氟沙星胶囊\n",
      "创建实体 盐酸利多卡因注射液\n",
      "创建实体 斯皮仁诺\n",
      "创建实体 安必君氨苄西林胶囊\n",
      "创建实体 丽珠奇乐\n",
      "创建实体 潜江制药阿奇霉素颗粒\n",
      "创建实体 蓉生药业人血白蛋白\n",
      "创建实体 孩尔欣头孢克洛颗粒\n",
      "创建实体 万托林\n",
      "创建实体 西藏甘露十一味金色丸\n",
      "创建实体 头孢氨苄甲氧苄啶片\n",
      "创建实体 广西灵峰药业蛇胆川贝液\n",
      "创建实体 珠海联邦头孢拉定胶囊\n",
      "创建实体 纽兰药业小青龙合剂\n",
      "创建实体 齐迈星\n",
      "创建实体 瑞尔医药盐酸班布特罗片\n",
      "创建实体 武汉生物人血白蛋白\n",
      "创建实体 吉春制药硝酸咪康唑乳膏\n",
      "创建实体 德源生物人血白蛋白\n",
      "创建实体 贵州汉方药业雷公藤多苷片\n",
      "创建实体 新亚注射用硫代硫酸钠\n",
      "创建实体 润阳药业盐酸氨溴索葡萄糖注\n",
      "创建实体 平光制药红霉素肠溶片\n",
      "创建实体 明水药业头孢氨苄甲氧苄啶片\n",
      "创建实体 迪沙药业甲硝唑维B6片\n",
      "创建实体 葵花药业得菲尔公司阿奇霉素\n",
      "创建实体 邦琪药业百咳静糖浆\n",
      "创建实体 汉森制药甲硝唑片\n",
      "创建实体 衡阳紫光古汉硝酸益康唑乳膏\n",
      "创建实体 注射用磺苄西林钠\n",
      "创建实体 海力制药阿奇霉素颗粒\n",
      "创建实体 莎普爱思盐酸氨溴索葡萄糖注\n",
      "创建实体 天津太平洋二羟丙茶碱片\n",
      "创建实体 博利康尼\n",
      "创建实体 广东卫伦人血白蛋白\n",
      "创建实体 易坦静\n",
      "创建实体 潇然\n",
      "创建实体 利君沙\n",
      "创建实体 远大医药黄石飞云制药雷公藤\n",
      "创建实体 旺龙药业琥乙红霉素颗粒\n",
      "创建实体 河南羚锐制药盐酸克林霉素胶\n",
      "创建实体 苏肽生\n",
      "创建实体 丽珠制药替硝唑片\n",
      "创建实体 桑海制药喷托维林氯化铵糖浆\n",
      "创建实体 二天堂联苯苄唑乳膏\n",
      "创建实体 利君制药阿奇霉素分散片\n",
      "创建实体 比利时辉瑞注射用甲泼尼龙琥\n",
      "创建实体 哈药总厂注射用磺苄西林钠\n",
      "创建实体 埃斯玛隆\n",
      "创建实体 广东环球制药茶碱缓释片\n",
      "创建实体 唐山红星硝酸咪康唑乳膏\n",
      "创建实体 福邦药业阿奇霉素胶囊\n",
      "创建实体 鲁抗赛特替硝唑片\n",
      "创建实体 沈阳一药替硝唑片\n",
      "创建实体 六安华源制药盐酸氨溴索葡萄\n",
      "创建实体 东药阿奇霉素胶囊\n",
      "创建实体 得恩德制药雷公藤多苷片\n",
      "创建实体 维宏\n",
      "创建实体 赣南制药喷托维林氯化铵糖浆\n",
      "创建实体 康正药业硝酸咪康唑乳膏\n",
      "创建实体 国瑞琥乙红霉素片\n",
      "创建实体 北海阳光药业阿奇霉素颗粒\n",
      "创建实体 百利药业阿奇霉素颗粒\n",
      "创建实体 双林生物人血白蛋白\n",
      "创建实体 亮博\n",
      "创建实体 普德药业注射用盐酸溴己新\n",
      "创建实体 运佳黄浦硝酸咪康唑乳膏\n",
      "创建实体 可尔生\n",
      "创建实体 人血白蛋白\n",
      "创建实体 诺健\n",
      "创建实体 傲宁\n",
      "创建实体 振兴氨茶碱片\n",
      "创建实体 星斗药业盐酸克林霉素胶囊\n",
      "创建实体 环科药业甲硝唑片\n",
      "创建实体 佐今明百咳静糖浆\n",
      "创建实体 黄河药业盐酸克林霉素胶囊\n",
      "创建实体 飞云岭药业甲硝唑片\n",
      "创建实体 同济堂制药盐酸克林霉素胶囊\n",
      "创建实体 赛乐欣\n",
      "创建实体 兰花药业诺氟沙星胶囊\n",
      "创建实体 银诺克肺宝蛤蚧定喘丸\n",
      "创建实体 信谊布地奈德气雾剂\n",
      "创建实体 吉林东方制药枸橼酸喷托维林\n",
      "创建实体 松泰斯\n",
      "创建实体 赣南海欣转移因子口服溶液\n",
      "创建实体 海力制药替硝唑片\n",
      "创建实体 大连药业托西酸舒他西林胶囊\n",
      "创建实体 央宗药业蒺藜药酒\n",
      "创建实体 三维康\n",
      "创建实体 伏枝\n",
      "创建实体 桂林南药布美他尼片\n",
      "创建实体 广春药业盐酸氨溴索葡萄糖注\n",
      "创建实体 遂成药业地塞米松磷酸钠注射\n",
      "创建实体 云南白药云南花粉片\n",
      "创建实体 罗欣药业注射用盐酸头孢替安\n",
      "创建实体 博康健制药盐酸氨溴索葡萄糖\n",
      "创建实体 美卓乐\n",
      "创建实体 东信药业蛇胆川贝液\n",
      "创建实体 欣得生\n",
      "创建实体 一品红盐酸克林霉素棕榈酸酯\n",
      "创建实体 新亚闵行盐酸克林霉素胶囊\n",
      "创建实体 吉尔吉药业喘嗽宁片\n",
      "创建实体 康良头孢拉定胶囊\n",
      "创建实体 天津力生制药硫酸沙丁胺醇片\n",
      "创建实体 东药盐酸克林霉素胶囊\n",
      "创建实体 哈药总厂头孢氨苄甲氧苄啶片\n",
      "创建实体 雄巴拉曲神水十一味金色丸\n",
      "创建实体 台城制药盐酸克林霉素胶囊\n",
      "创建实体 绿十字\n",
      "创建实体 锦华药业诺氟沙星胶囊\n",
      "创建实体 利君制药琥乙红霉素片\n",
      "创建实体 石药欧意枸橼酸喷托维林片\n",
      "创建实体 江西诚志永丰蛇胆川贝液\n",
      "创建实体 康美药业红霉素肠溶片\n",
      "创建实体 萨兰欣\n",
      "创建实体 山西云鹏制药氨茶碱片\n",
      "创建实体 云门药业头孢氨苄甲氧苄啶片\n",
      "创建实体 亚宝药业甲硝唑片\n",
      "创建实体 美扶\n",
      "创建实体 阿莫仙阿莫西林颗粒\n",
      "创建实体 莱士人血白蛋白\n",
      "创建实体 丰原制药注射用盐酸溴己新\n",
      "创建实体 辽宁海思科注射用甲泼尼龙琥\n",
      "创建实体 白云山光华头孢氨苄甲氧苄啶\n",
      "创建实体 太原药业甲硝唑片\n",
      "创建实体 恩经复\n",
      "创建实体 仁和药业盐酸克林霉素胶囊\n",
      "创建实体 东信药业阿奇霉素颗粒\n",
      "创建实体 诚信制药盐酸妥洛特罗片\n",
      "创建实体 联邦左福康盐酸左氧氟沙星胶\n",
      "创建实体 华中药业甲硝唑片\n",
      "创建实体 彼迪药业阿奇霉素分散片\n",
      "创建实体 天方药业盐酸克林霉素胶囊\n",
      "创建实体 天天乐药业蛇胆川贝液\n",
      "创建实体 普洛康裕天然药物雷公藤多苷\n",
      "创建实体 石药欧意红霉素肠溶片\n",
      "创建实体 达发新\n",
      "创建实体 鲁抗阿莫西林颗粒\n",
      "创建实体 达力芬\n",
      "创建实体 白云山天心地塞米松磷酸钠注\n",
      "创建实体 鲁神乙酰螺旋霉素片\n",
      "创建实体 鲁抗注射用头孢唑林钠\n",
      "创建实体 彼迪药业替硝唑片\n",
      "创建实体 皇隆制药注射用呋塞米\n",
      "创建实体 重庆华邦制药注射用甲泼尼龙\n",
      "创建实体 智同生物注射用盐酸溴己新\n",
      "创建实体 永定制药百咳静糖浆\n",
      "创建实体 金诃藏药大月晶丸\n",
      "创建实体 津华晖星替硝唑片\n",
      "创建实体 科伦药业盐酸克林霉素胶囊\n",
      "创建实体 帮备\n",
      "创建实体 锦华药业盐酸克林霉素胶囊\n",
      "创建实体 纽兰药业硝酸咪康唑栓\n",
      "创建实体 泰邦生物人血白蛋白\n",
      "创建实体 健生制药阿奇霉素分散片\n",
      "创建实体 益佳清\n",
      "创建实体 蜀阳药业人血白蛋白\n",
      "创建实体 唐山红星硝酸益康唑溶液\n",
      "创建实体 天丁噻托溴铵粉雾剂\n",
      "创建实体 甲强龙\n",
      "创建实体 美通制药雷公藤多苷片\n",
      "创建实体 亚太药业盐酸克林霉素胶囊\n",
      "创建实体 一品红注射用盐酸溴己新\n",
      "创建实体 盐酸克林霉素胶囊\n",
      "创建实体 开林\n",
      "创建实体 福建太平洋制药酮康唑乳膏\n",
      "创建实体 辅仁药业阿奇霉素片\n",
      "创建实体 米乐松\n",
      "创建实体 上海新亚头孢克肟分散片\n",
      "创建实体 鲁抗麦迪霉素片\n",
      "创建实体 吉春制药祛痰止咳胶囊\n",
      "创建实体 迪龙制药胸腺肽肠溶片\n",
      "创建实体 石药欧意氨茶碱片\n",
      "创建实体 金耀药业地塞米松磷酸钠注射\n",
      "创建实体 罗欣药业注射用盐酸氨溴索\n",
      "创建实体 浙江南洋药业阿奇霉素胶囊\n",
      "创建实体 金诃藏药仁青芒觉\n",
      "创建实体 仙琚制药克霉唑阴道片\n",
      "创建实体 迪沙药业克霉唑阴道片\n",
      "创建实体 倍佳\n",
      "创建实体 尤金\n",
      "创建实体 全星制药注射用盐酸头孢替安\n",
      "创建实体 西藏甘露仁青芒觉\n",
      "创建实体 海鲸药业注射用盐酸溴己新\n",
      "创建实体 蛇胆川贝液\n",
      "创建实体 锋替新\n",
      "创建实体 希舒美\n",
      "创建实体 复旦复华雷公藤多苷片\n",
      "创建实体 圣泰转移因子口服溶液\n",
      "创建实体 七星山药业化痰平喘片\n",
      "创建实体 海王阿奇霉素片\n",
      "创建实体 注射用盐酸头孢替安\n",
      "创建实体 东新药业百咳静糖浆\n",
      "创建实体 白云山医药琥乙红霉素片\n",
      "创建实体 金路捷\n",
      "创建实体 卓峰地塞米松磷酸钠注射液\n",
      "创建实体 晶珠藏药蛇胆川贝液\n",
      "创建实体 西岳制药托西酸舒他西林胶囊\n",
      "创建实体 曲靖药业白及颗粒\n",
      "创建实体 北京永康药业盐酸利多卡因注\n",
      "创建实体 五洲通药业硝酸咪康唑乳膏\n",
      "创建实体 新陇海药业雷公藤多苷片\n",
      "创建实体 济安堂蛇胆川贝液\n",
      "创建实体 康裕头孢氨苄胶囊\n",
      "创建实体 迪诺制药替硝唑片\n",
      "创建实体 德远药业雷公藤多苷片\n",
      "创建实体 朗瑞\n",
      "创建实体 海斯制药注射用盐酸溴己新\n",
      "创建实体 艾富西药业盐酸氨溴索葡萄糖\n",
      "创建实体 北京中新制药琥乙红霉素片\n",
      "创建实体 东信药业琥乙红霉素颗粒\n",
      "创建实体 涟水制药地塞米松磷酸钠注射\n",
      "创建实体 银力舒\n",
      "创建实体 利民制药阿奇霉素胶囊\n",
      "创建实体 赤峰维康托西酸舒他西林胶囊\n",
      "创建实体 山西康宝人血白蛋白\n",
      "创建实体 津华晖星甲硝唑片\n",
      "创建实体 首儿药厂小青龙颗粒\n",
      "创建实体 亨瑞达制药甲硝唑片\n",
      "创建实体 安诺药业盐酸克林霉素胶囊\n",
      "创建实体 彼迪药业托西酸舒他西林胶囊\n",
      "创建实体 南通中华药业硝酸咪康唑乳膏\n",
      "创建实体 思力华\n",
      "创建实体 东药替硝唑片\n",
      "创建实体 圣大（张家口）头孢氨苄甲氧\n",
      "创建实体 利君制药红霉素肠溶片\n",
      "创建实体 卫光生物人血白蛋白\n",
      "创建实体 澳利达奈德注射用盐酸溴己新\n",
      "创建实体 国医堂制药如意定喘丸\n",
      "创建实体 欣匹特\n",
      "创建实体 宜昌人福盐酸克林霉素胶囊\n",
      "创建实体 达仁堂通宣理肺丸\n",
      "创建实体 衡博来\n",
      "创建实体 甲硝唑片\n",
      "创建实体 北京曙光药业红霉素肠溶片\n",
      "创建实体 新峰药业盐酸利多卡因注射液\n",
      "创建实体 海斯制药盐酸利多卡因注射液\n",
      "创建实体 金峰制药盐酸氨溴索葡萄糖注\n",
      "创建实体 傲群\n",
      "创建实体 黑龙江天龙药业联苯苄唑乳膏\n",
      "创建实体 锦华药业氯霉素片\n",
      "创建实体 华南药业红霉素肠溶片\n",
      "创建实体 北京同仁堂百咳静糖浆\n",
      "创建实体 全星制药注射用布美他尼\n",
      "创建实体 俞氏阿奇霉素颗粒\n",
      "创建实体 陕西东泰制药复方蛤青胶囊\n",
      "创建实体 龙泰制药化痰平喘片\n",
      "创建实体 奇莫欣\n",
      "创建实体 金耀药业注射用甲泼尼龙琥珀\n",
      "创建实体 东信药业琥乙红霉素片\n",
      "创建实体 汇仁药业阿奇霉素片\n",
      "创建实体 惠普森穿心莲内酯片\n",
      "创建实体 大连天宇制药阿奇霉素胶囊\n",
      "创建实体 恒安药业盐酸特比萘芬片\n",
      "创建实体 宜昌长江乳酸左氧氟沙星片\n"
     ]
    }
   ],
   "source": [
    "for each in producers:\n",
    "    node = Node('Producer', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建 症状 实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 谵妄\n",
      "创建实体 目赤\n",
      "创建实体 发烧\n",
      "创建实体 咯血\n",
      "创建实体 眼睛痒\n",
      "创建实体 咽部异物感\n",
      "创建实体 纵隔浊音界扩大\n",
      "创建实体 化学性支气管炎\n",
      "创建实体 咳铁锈色痰\n",
      "创建实体 咳嗽伴胸痛\n",
      "创建实体 紧张性头晕\n",
      "创建实体 冬春季的慢性咳...\n",
      "创建实体 腱反射消失\n",
      "创建实体 气急\n",
      "创建实体 多发性神经炎\n",
      "创建实体 乏力\n",
      "创建实体 发热伴咳嗽、咯...\n",
      "创建实体 呼气音延长\n",
      "创建实体 腹泻\n",
      "创建实体 咳嗽伴哮鸣音\n",
      "创建实体 呼吸音减弱\n",
      "创建实体 肺阴虚\n",
      "创建实体 闫铁\n",
      "创建实体 闫鹏辉\n",
      "创建实体 骨膜炎\n",
      "创建实体 间歇性头晕\n",
      "创建实体 低热\n",
      "创建实体 面色青紫\n",
      "创建实体 桶状胸\n",
      "创建实体 体重减轻\n",
      "创建实体 痰中带血丝\n",
      "创建实体 急性面容\n",
      "创建实体 呕吐\n",
      "创建实体 口唇和甲床略带青紫\n",
      "创建实体 紧张性头痛\n",
      "创建实体 肺纹理增粗\n",
      "创建实体 间歇性头痛\n",
      "创建实体 肺纤维化\n",
      "创建实体 蛋白尿\n",
      "创建实体 湿啰音\n",
      "创建实体 咯血伴发热\n",
      "创建实体 咳出棕色痰栓\n",
      "创建实体 肺泡炎症\n",
      "创建实体 盗汗\n",
      "创建实体 纤毛上皮细胞损伤脱落\n",
      "创建实体 畏寒\n",
      "创建实体 横膈低平\n",
      "创建实体 黏稠或脓性痰伴...\n",
      "创建实体 昏迷\n",
      "创建实体 气短\n",
      "创建实体 惊厥\n",
      "创建实体 肺泡灌洗液可见...\n",
      "创建实体 胸闷憋气\n",
      "创建实体 小支气管粘膜水肿\n",
      "创建实体 紫绀\n",
      "创建实体 呼吸困难\n",
      "创建实体 哮鸣音\n",
      "创建实体 喘息\n",
      "创建实体 心源性呼吸窘迫\n",
      "创建实体 鼻塞\n",
      "创建实体 肺部啰音\n",
      "创建实体 嗜酸性粒细胞增多\n",
      "创建实体 胸痛\n",
      "创建实体 耸肩喘息\n",
      "创建实体 感觉障碍\n",
      "创建实体 痉挛性咳嗽\n",
      "创建实体 出生后即有持续青紫\n",
      "创建实体 毓卓\n",
      "创建实体 抽搐\n",
      "创建实体 痰呈粘液脓性\n",
      "创建实体 头晕\n",
      "创建实体 口唇青紫\n",
      "创建实体 吸气时有蝉鸣音\n",
      "创建实体 恶心\n",
      "创建实体 浅感觉减退或缺失\n",
      "创建实体 肺部出血\n",
      "创建实体 变应性咳嗽\n",
      "创建实体 胸闷\n"
     ]
    }
   ],
   "source": [
    "for each in symptoms:\n",
    "    node = Node('Symptom', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建知识图谱关系（连接、边）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Neo4j样例代码\n",
    "\n",
    "# # 删除所有实体和关系\n",
    "# cypher = 'MATCH (n) DETACH DELETE n'\n",
    "# g.run(cypher)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 创建关系 样例代码\n",
    "# start_node = 'Disease'\n",
    "# end_node = 'Check'\n",
    "# p = '百日咳'\n",
    "# q = '血常规'\n",
    "# rel_type = 'need_check'\n",
    "# rel_name = '诊断检查'\n",
    "\n",
    "# # Cypher语句\n",
    "# query = \"match(p:%s),(q:%s) where p.name='%s' and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)\" % (start_node, end_node, p, q, rel_type, rel_name)\n",
    "# print(query)\n",
    "# g.run(query) # 运行 Cypher 语句\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_relationship(start_node, end_node, edges, rel_type, rel_name):\n",
    "    '''创建关系函数'''\n",
    "    for edge in edges:\n",
    "        p = edge[0]\n",
    "        q = edge[1]\n",
    "        # 创建关系的 Cypher 语句\n",
    "        query = \"match(p:%s),(q:%s) where p.name='%s' and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)\" % (start_node, end_node, p, q, rel_type, rel_name)\n",
    "        try:\n",
    "            g.run(query) # 运行 Cypher 语句\n",
    "            print('创建关系 {}-{}->{}'.format(p, rel_type, q))\n",
    "        except Exception as e:\n",
    "            print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建所有关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建关系 肺泡蛋白质沉积症-has_symptom->紫绀\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->胸痛\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->呼吸困难\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->乏力\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->毓卓\n",
      "创建关系 百日咳-has_symptom->吸气时有蝉鸣音\n",
      "创建关系 百日咳-has_symptom->痉挛性咳嗽\n",
      "创建关系 百日咳-has_symptom->胸闷\n",
      "创建关系 百日咳-has_symptom->肺阴虚\n",
      "创建关系 百日咳-has_symptom->抽搐\n",
      "创建关系 百日咳-has_symptom->低热\n",
      "创建关系 百日咳-has_symptom->闫鹏辉\n",
      "创建关系 百日咳-has_symptom->惊厥\n",
      "创建关系 苯中毒-has_symptom->恶心\n",
      "创建关系 苯中毒-has_symptom->抽搐\n",
      "创建关系 苯中毒-has_symptom->感觉障碍\n",
      "创建关系 喘息样支气管炎-has_symptom->耸肩喘息\n",
      "创建关系 喘息样支气管炎-has_symptom->哮鸣音\n",
      "创建关系 喘息样支气管炎-has_symptom->纤毛上皮细胞损伤脱落\n",
      "创建关系 喘息样支气管炎-has_symptom->变应性咳嗽\n",
      "创建关系 喘息样支气管炎-has_symptom->化学性支气管炎\n",
      "创建关系 喘息样支气管炎-has_symptom->喘息\n",
      "创建关系 喘息样支气管炎-has_symptom->冬春季的慢性咳...\n",
      "创建关系 喘息样支气管炎-has_symptom->咳嗽伴哮鸣音\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->呼吸困难\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->紫绀\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->心源性呼吸窘迫\n",
      "创建关系 大量羊水吸入-has_symptom->面色青紫\n",
      "创建关系 大量羊水吸入-has_symptom->呼吸困难\n",
      "创建关系 大量羊水吸入-has_symptom->口唇青紫\n",
      "创建关系 大量羊水吸入-has_symptom->肺纹理增粗\n",
      "创建关系 大量羊水吸入-has_symptom->出生后即有持续青紫\n",
      "创建关系 大量羊水吸入-has_symptom->口唇和甲床略带青紫\n",
      "创建关系 大量羊水吸入-has_symptom->气急\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->嗜酸性粒细胞增多\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->咽部异物感\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->胸闷憋气\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->咯血伴发热\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->胸闷\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->乏力\n",
      "创建关系 大叶性肺炎-has_symptom->湿啰音\n",
      "创建关系 大叶性肺炎-has_symptom->胸痛\n",
      "创建关系 大叶性肺炎-has_symptom->发烧\n",
      "创建关系 大叶性肺炎-has_symptom->咳铁锈色痰\n",
      "创建关系 大叶性肺炎-has_symptom->急性面容\n",
      "创建关系 大叶性肺炎-has_symptom->呼吸音减弱\n",
      "创建关系 大楼病综合征-has_symptom->紧张性头晕\n",
      "创建关系 大楼病综合征-has_symptom->紧张性头痛\n",
      "创建关系 大楼病综合征-has_symptom->间歇性头晕\n",
      "创建关系 大楼病综合征-has_symptom->鼻塞\n",
      "创建关系 大楼病综合征-has_symptom->头晕\n",
      "创建关系 大楼病综合征-has_symptom->恶心\n",
      "创建关系 大楼病综合征-has_symptom->眼睛痒\n",
      "创建关系 大楼病综合征-has_symptom->目赤\n",
      "创建关系 大楼病综合征-has_symptom->间歇性头痛\n",
      "创建关系 二硫化碳中毒-has_symptom->昏迷\n",
      "创建关系 二硫化碳中毒-has_symptom->腱反射消失\n",
      "创建关系 二硫化碳中毒-has_symptom->呕吐\n",
      "创建关系 二硫化碳中毒-has_symptom->谵妄\n",
      "创建关系 二硫化碳中毒-has_symptom->多发性神经炎\n",
      "创建关系 二硫化碳中毒-has_symptom->浅感觉减退或缺失\n",
      "创建关系 二硫化碳中毒-has_symptom->恶心\n",
      "创建关系 二硫化碳中毒-has_symptom->感觉障碍\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->盗汗\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->腹泻\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->乏力\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->肺部出血\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->呼吸困难\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->咯血\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->气短\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->蛋白尿\n",
      "创建关系 肺放线菌病-has_symptom->骨膜炎\n",
      "创建关系 肺放线菌病-has_symptom->体重减轻\n",
      "创建关系 肺放线菌病-has_symptom->咯血\n",
      "创建关系 肺放线菌病-has_symptom->盗汗\n",
      "创建关系 肺放线菌病-has_symptom->乏力\n",
      "创建关系 肺放线菌病-has_symptom->低热\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->胸痛\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->紫绀\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->呼吸困难\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->肺泡炎症\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->肺泡灌洗液可见...\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->乏力\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺曲菌病-has_symptom->咳出棕色痰栓\n",
      "创建关系 肺曲菌病-has_symptom->嗜酸性粒细胞增多\n",
      "创建关系 肺曲菌病-has_symptom->胸痛\n",
      "创建关系 放射性肺炎-has_symptom->闫铁\n",
      "创建关系 放射性肺炎-has_symptom->肺纤维化\n",
      "创建关系 放射性肺炎-has_symptom->胸痛\n",
      "创建关系 放射性肺炎-has_symptom->低热\n",
      "创建关系 肺念珠菌病-has_symptom->痰呈粘液脓性\n",
      "创建关系 肺念珠菌病-has_symptom->肺部啰音\n",
      "创建关系 肺大疱-has_symptom->胸闷\n",
      "创建关系 肺大疱-has_symptom->气短\n",
      "创建关系 肺大疱-has_symptom->胸闷憋气\n",
      "创建关系 肺大疱-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺炎球菌肺炎-has_symptom->痰中带血丝\n",
      "创建关系 肺炎球菌肺炎-has_symptom->咳嗽伴胸痛\n",
      "创建关系 肺炎球菌肺炎-has_symptom->胸痛\n",
      "创建关系 肺炎球菌肺炎-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺气肿-has_symptom->纵隔浊音界扩大\n",
      "创建关系 肺气肿-has_symptom->桶状胸\n",
      "创建关系 肺气肿-has_symptom->小支气管粘膜水肿\n",
      "创建关系 肺气肿-has_symptom->黏稠或脓性痰伴...\n",
      "创建关系 肺气肿-has_symptom->横膈低平\n",
      "创建关系 肺气肿-has_symptom->胸闷\n",
      "创建关系 肺气肿-has_symptom->呼气音延长\n",
      "创建关系 肺气肿-has_symptom->肺部啰音\n",
      "创建关系 肺气肿-has_symptom->呼吸音减弱\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->畏寒\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->胸痛\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->气急\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->发热伴咳嗽、咯...\n"
     ]
    }
   ],
   "source": [
    "# create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', '推荐食谱')\n",
    "# create_relationship('Disease', 'Food', rels_noteat, 'no_eat', '忌吃')\n",
    "# create_relationship('Disease', 'Food', rels_doeat, 'do_eat', '宜吃')\n",
    "# create_relationship('Department', 'Department', rels_department, 'belongs_to', '属于')\n",
    "# create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', '常用药品')\n",
    "# create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', '生产药品')\n",
    "# create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', '好评药品')\n",
    "# create_relationship('Disease', 'Check', rels_check, 'need_check', '诊断检查')\n",
    "create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', '症状')\n",
    "# create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', '并发症')\n",
    "# create_relationship('Disease', 'Department', rels_category, 'belongs_to', '所属科室')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
